using System.Collections.Generic;
using System.Linq;
using I2.Loc;
using UnityEngine;
using UnityEngine.Advertisements;
using UnityEngine.UI;

public class PlaygroundSetting : MonoBehaviour
{
	public static PlaygroundSetting Instance;

	public Player player;

	public Canvas canvas;

	public Slider speedSlider;

	public Text speedText;

	public Slider strengthSlider;

	public Text strengthText;

	public Slider kickSlider;

	public Text kickText;

	public Toggle unlimitedAmmoToggle;

	public Slider totalZombieSlider;

	public Text totalZombieText;

	public Slider zombieDistanceSlider;

	public Text zombieDistanceText;

	public Slider waveIntervalSlider;

	public Text waveIntervalText;

	public Text tooManyWarning;

	public GameObject primaryWeaponSlot;

	public GameObject secondaryWeaponSlot;

	public GameObject meleeWeaponSlot;

	public GameObject suitSlot;

	public GameObject itemList;

	public GameObject itemViewPrefab;

	public GameObject zombieList;

	public GameObject zombieViewPrefab;

	public GameObject toolsTipsPrefab;

	public Dictionary<string, int> zombiesWeight = new Dictionary<string, int>();

	public string onSelectWeapoSlot;

	public float adsNextShowTime;

	private void Awake()
	{
		Instance = this;
	}

	private void Start()
	{
		Init();
		SettingCheck();
	}

	private void Init()
	{
		speedSlider.minValue = 2f;
		speedSlider.maxValue = Config.playerMaxSpeed;
		speedSlider.onValueChanged.AddListener(delegate (float value)
		{
			UpdateSpeed(value);
		});
		speedSlider.value = DO.Playground.playerSpeed;
		UpdateSpeed(DO.Playground.playerSpeed);
		strengthSlider.minValue = 2f;
		strengthSlider.maxValue = Config.playerMaxStrength;
		strengthSlider.onValueChanged.AddListener(delegate (float value)
		{
			UpdateStrength(value);
		});
		strengthSlider.value = DO.Playground.playerStrength;
		UpdateStrength(DO.Playground.playerStrength);
		kickSlider.minValue = 1f;
		kickSlider.maxValue = Config.playerMaxKickTotal;
		kickSlider.onValueChanged.AddListener(delegate (float value)
		{
			UpdateKickTotal((int)value);
		});
		kickSlider.value = DO.Playground.playerKickTotal;
		UpdateKickTotal(DO.Playground.playerKickTotal);
		unlimitedAmmoToggle.onValueChanged.AddListener(delegate (bool value)
		{
			UpdateUnlimitedAmmo(value);
		});
		unlimitedAmmoToggle.isOn = DO.Playground.unlimitedAmmo;
		totalZombieSlider.minValue = 0f;
		totalZombieSlider.maxValue = Config.playgroundMaxTotalZombie;
		totalZombieSlider.onValueChanged.AddListener(delegate (float value)
		{
			UpdateTotalZombie((int)value);
		});
		totalZombieSlider.value = DO.Playground.totalZombiePreWave;
		UpdateTotalZombie(DO.Playground.totalZombiePreWave);
		zombieDistanceSlider.onValueChanged.AddListener(delegate (float value)
		{
			UpdateZombieDistance((int)value);
		});
		zombieDistanceSlider.value = DO.Playground.zombieDistance;
		UpdateZombieDistance(DO.Playground.zombieDistance);
		waveIntervalSlider.onValueChanged.AddListener(delegate (float value)
		{
			UpdateWaveInterval((int)value);
		});
		waveIntervalSlider.value = DO.Playground.waveInterval;
		UpdateWaveInterval(DO.Playground.waveInterval);
		SetItemToSlot(CampaignGame.itemCache[DO.Playground.primaryWeapon], primaryWeaponSlot);
		SetItemToSlot(CampaignGame.itemCache[DO.Playground.secondaryWeapon], secondaryWeaponSlot);
		SetItemToSlot(CampaignGame.itemCache[DO.Playground.meleeWeapon], meleeWeaponSlot);
		SetItemToSlot(CampaignGame.itemCache[DO.Playground.suit], suitSlot);
		SetItemList();
		SetZombieList();
		RefreshWave();
		player.campaignGame.animator.Play("Game");
		if (player.campaignGame.toolsView.transform.childCount < 4)
		{
			Object.Instantiate(toolsTipsPrefab, player.campaignGame.toolsView.transform).transform.GetChild(0).GetComponent<Button>().onClick.AddListener(delegate
			{
				GotoCampaign();
			});
		}
	}

	public void SetZombieList()
	{
		Zombie[] array = (from x in Resources.LoadAll("Zombies/", typeof(Zombie)).Cast<Zombie>().ToArray()
						  orderby x.deployAfterDay
						  select x).ToArray();
		Zombie[] array2 = array;
		foreach (Zombie zombie2 in array2)
		{
			zombiesWeight.Add(zombie2.name, 0);
		}
		foreach (string item in DO.Playground.zombiesWeight)
		{
			string[] array3 = item.Split(',');
			string key = array3[0];
			int value2 = int.Parse(array3[1]);
			zombiesWeight[key] = value2;
		}
		array2 = array;
		foreach (Zombie zombie in array2)
		{
			GameObject obj = Object.Instantiate(zombieViewPrefab, zombieList.transform);
			Text component = obj.transform.Find("Text").GetComponent<Text>();
			component.text = LocalizationManager.GetTranslation(zombie.name);
			Slider component2 = obj.transform.Find("Slider").GetComponent<Slider>();
			if (DO.Player.reachedDay >= zombie.deployAfterDay)
			{
				component2.value = zombiesWeight[zombie.name];
				component2.onValueChanged.AddListener(delegate (float value)
				{
					zombiesWeight[zombie.name] = (int)value;
				});
			}
			else
			{
				component2.value = 0f;
				zombiesWeight[zombie.name] = 0;
				Object.Destroy(component2.gameObject);
				component.GetComponent<RectTransform>().sizeDelta = new Vector2(150f, 50f);
				component.text = component.text + "\n<color=\"#FFB200\"><size=16>" + LocalizationManager.GetTranslation("UnlockFromGame") + "</size></color>";
			}
		}
	}

	public void SetItemList()
	{
		KeyValuePair<string, Item>[] array = CampaignGame.itemCache.OrderBy((KeyValuePair<string, Item> x) => x.Value.buyableAfterDay).ToArray();
		for (int i = 0; i < array.Length; i++)
		{
			KeyValuePair<string, Item> kvp = array[i];
			GameObject gameObject = Object.Instantiate(itemViewPrefab, itemList.transform);
			gameObject.name = kvp.Key;
			gameObject.transform.Find("Text").GetComponent<Text>().text = kvp.Value.Title;
			Button component = gameObject.transform.Find("Button").GetComponent<Button>();
			SetItemToSlot(kvp.Value, component.gameObject);
			if (!DO.Player.reachedItems.Contains(kvp.Key))
			{
				gameObject.transform.Find("Locked").gameObject.SetActive(value: true);
				continue;
			}
			component.onClick.AddListener(delegate
			{
				if (kvp.Value.type == ItemType.Weapon)
				{
					if (onSelectWeapoSlot == "Primary")
					{
						ChangePlayerPrimaryWeapon(kvp.Key);
					}
					else if (onSelectWeapoSlot == "Secondary")
					{
						ChangePlayerSecondaryWeapon(kvp.Key);
					}
				}
				else if (kvp.Value.type == ItemType.MeleeWeapon)
				{
					ChangePlayerMeleeWeapon(kvp.Key);
				}
				else if (kvp.Value.type == ItemType.Suit)
				{
					ChangePlayerSuit(kvp.Key);
				}
				itemList.transform.parent.gameObject.SetActive(value: false);
			});
		}
	}

	public void OpenItemList(string slot)
	{
		onSelectWeapoSlot = slot;
		ItemType itemType = ((slot == "Melee") ? ItemType.MeleeWeapon : ((slot == "Suit") ? ItemType.Suit : ItemType.Weapon));
		foreach (Transform item in itemList.transform)
		{
			if (CampaignGame.itemCache.ContainsKey(item.name) && CampaignGame.itemCache[item.name].type != itemType)
			{
				item.gameObject.SetActive(value: false);
			}
			else
			{
				item.gameObject.SetActive(value: true);
			}
		}
		itemList.transform.parent.gameObject.SetActive(value: true);
	}

	public void ChangePlayerPrimaryWeapon(string item)
	{
		DO.Playground.primaryWeapon = item;
		SetItemToSlot(CampaignGame.itemCache[item], primaryWeaponSlot);
		player.EquipAll();
		if (player.body.primaryWeaponHolder.transform.childCount > 0)
		{
			Object.Destroy(player.body.primaryWeaponHolder.transform.GetChild(0).gameObject);
		}
	}

	public void ChangePlayerSecondaryWeapon(string item)
	{
		DO.Playground.secondaryWeapon = item;
		SetItemToSlot(CampaignGame.itemCache[item], secondaryWeaponSlot);
		player.EquipAll();
		if (player.body.primaryWeaponHolder.transform.childCount > 0)
		{
			Object.Destroy(player.body.primaryWeaponHolder.transform.GetChild(0).gameObject);
		}
	}

	public void ChangePlayerMeleeWeapon(string item)
	{
		DO.Playground.meleeWeapon = item;
		SetItemToSlot(CampaignGame.itemCache[item], meleeWeaponSlot);
		player.EquipAll();
	}

	public void ChangePlayerSuit(string item)
	{
		DO.Playground.suit = item;
		SetItemToSlot(CampaignGame.itemCache[item], suitSlot);
		player.EquipAll();
	}

	public void SetItemToSlot(Item item, GameObject slot)
	{
		if (slot.transform.childCount > 0)
		{
			Object.Destroy(slot.transform.GetChild(0).gameObject);
		}
		Object.Instantiate(item, slot.transform).transform.localScale *= 60f;
	}

	public void UpdateSpeed(float value)
	{
		DO.Playground.playerSpeed = value;
		speedText.text = LocalizationManager.GetTranslation("Speed") + ": <color=#FFB200>" + value.ToString("n1") + "</color>";
		if ((bool)player && (bool)player.body)
		{
			player.speed = value + player.body.speed;
			if ((bool)player.weapon)
			{
				player.weapon.animator.SetFloat("speed", 0.5f + player.speed / 4f);
			}
			player.body.animator.SetFloat("speed", player.speed);
		}
	}

	public void UpdateStrength(float value)
	{
		DO.Playground.playerStrength = value;
		strengthText.text = LocalizationManager.GetTranslation("Strength") + ": <color=#FFB200>" + value.ToString("n1") + "</color>";
		if ((bool)player && (bool)player.body)
		{
			player.strength = value + player.body.strength;
		}
	}

	public void UpdateKickTotal(int value)
	{
		DO.Playground.playerKickTotal = value;
		kickText.text = LocalizationManager.GetTranslation("KickLevel") + ": <color=#FFB200>" + value.ToString("n0") + "</color>";
		if ((bool)player && (bool)player.body)
		{
			player.kickTotal = value + player.body.kick;
		}
	}

	public void UpdateUnlimitedAmmo(bool value)
	{
		DO.Playground.unlimitedAmmo = value;
		if (!DO.Playground.unlimitedAmmo && (bool)player.weapon)
		{
			player.weapon.ammo = player.weapon.capacity;
		}
	}

	public void UpdateTotalZombie(int value)
	{
		DO.Playground.totalZombiePreWave = value;
		totalZombieText.text = LocalizationManager.GetTranslation("ZombieQty") + ": <color=#FFB200>" + value.ToString("n0") + "</color>";
		tooManyWarning.gameObject.SetActive(value > 30);
	}

	public void UpdateZombieDistance(int value)
	{
		DO.Playground.zombieDistance = value;
		zombieDistanceText.text = LocalizationManager.GetTranslation("Distance") + ": <color=#FFB200>" + value.ToString("n0") + "m</color>";
	}

	public void UpdateWaveInterval(int value)
	{
		DO.Playground.waveInterval = value;
		player.campaignGame.restTimePreWave = value;
		waveIntervalText.text = LocalizationManager.GetTranslation("Interval") + ": <color=#FFB200>" + value.ToString("n0") + " sec</color>";
	}

	public void Open()
	{
		AdManager.Instance.ShowNative(0f);
		if (CampaignGame.Instance.toolsConfirmView.gameObject.activeSelf)
		{
			CampaignGame.Instance.CancelPlaceTools();
		}
		canvas.gameObject.SetActive(value: true);
		Time.timeScale = 0f;
	}

	public void Apply()
	{
		Close();
	}

	public void Close()
	{
		SavePlayground();
		canvas.gameObject.SetActive(value: false);
		Time.timeScale = 1f;
	}

	public void SavePlayground()
	{
		List<string> list = new List<string>();
		foreach (KeyValuePair<string, int> item in zombiesWeight)
		{
			list.Add(item.Key + "," + item.Value);
		}
		DO.Playground.zombiesWeight = list;
		SaveLoad.SaveString("Playground", JsonUtility.ToJson(DO.Playground));
		SettingCheck();
	}

	public void SettingCheck()
	{
		int num = 0;
		foreach (KeyValuePair<string, int> item in zombiesWeight)
		{
			num += item.Value;
		}
		base.transform.Find("Warning").gameObject.SetActive(DO.Playground.totalZombiePreWave == 0 || num == 0);
	}

	public void RefreshWave()
	{
		GameObject[] array = GameObject.FindGameObjectsWithTag("Zombie");
		for (int i = 0; i < array.Length; i++)
		{
			Object.Destroy(array[i]);
		}
		CampaignGame.zombieKillCount = player.campaignGame.zombieTotal;
		player.campaignGame.nextWaveTime = Time.time + (float)DO.Playground.waveInterval;
		SetZombieSeed();
		Close();
	}

	public void RefreshMap()
	{
		Close();
		Initiate.Fade("PlaygroundGame", Color.black, 5f);
	}

	public void ClearMap()
	{
		DO.Playground.stageItems = new List<StageItem>();
		Initiate.Fade("PlaygroundGame", Color.black, 5f);
		Close();
	}

	public void Exit()
	{
		Initiate.Fade("Main", Color.black, 5f);
		Close();
	}

	public void SetZombieSeed()
	{
		player.campaignGame.zombieSeed.Clear();
		Object[] array = Resources.LoadAll("Zombies/", typeof(Object));
		for (int i = 0; i < array.Length; i++)
		{
			Zombie component = ((GameObject)array[i]).GetComponent<Zombie>();
			if (zombiesWeight[component.name] > 0)
			{
				for (int j = 0; j < zombiesWeight[component.name]; j++)
				{
					player.campaignGame.zombieSeed.Add(component);
				}
			}
		}
	}

	public void ShowInterstitialAd()
	{
	}

	public void GotoCampaign()
	{
		DO.ReLoadCampaign();
		if (DO.Campaign.dayFinish && !DO.Campaign.dayShoppingFinish)
		{
			Initiate.Fade("CampaignShop", Color.black, 5f);
		}
		else if (!DO.Campaign.dayFinish && DO.Campaign.dayShoppingFinish)
		{
			Initiate.Fade("CampaignPrepare", Color.black, 5f);
		}
		else
		{
			Initiate.Fade("CampaignGame", Color.black, 5f);
		}
	}
}
